<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
require_once dirname(__FILE__) . '/../../../incls/php/ecomutils.class.php';

session_name(SESSION);
session_start();

$myPdo = DbUtils::createPdoInst();
$page_id = MiscUtils::getParam('i', 0);
$instance_id = MiscUtils::getParam('inid', 0);
$my_order = MiscUtils::getParam('my_order', 0);
$order_id = MiscUtils::getParam('pay', 0);

$cond_vals = new stdClass();
$cond_vals->c = 't.order_number = :v1';
$cond_vals->v = array(':v1' => $order_id);
$opt = new stdClass();
$opt->select_expr = 't.*, t1.description AS _description';
$opt->join_expr = 'LEFT JOIN ' . TABLE_NAME_PREFIX_PLUGIN . 'ecom_mthd_payment AS t1 ON t1.id = t.payment_id';
$tmp = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'ecom_order', $cond_vals, NULL, NULL, NULL, NULL, $opt)->d;

$profile = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . 'user_profile', NULL, NULL, NULL, 1, 1, NULL)->d[0];

?>
<!DOCTYPE html>
<html>
<head>
<script src="../../../libs/jquery/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/json/json2.js"></script>
<script src="../../../libs/jquery_ui/js/jquery-ui-1.10.2.custom.js"></script>
<link href="jquery_ui/css/smoothness/jquery-ui-1.10.2.custom.css" rel="stylesheet">
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
}
p {
  padding: 0;
  margin: 0;
}
.imgbtn {
  background-image: url(images/visa.png);
  background-repeat: no-repeat;
  background-position: 5px 2px;
  padding: 5px 5px 5px 30px;
  border: 1px solid #666;
  background-color: #fff;
  cursor: pointer;
  border-radius: 3px;
  height: 55px;
  width: 130px;
  padding-left: 45px;
  font-family: Lucida Sans Unicode;
}
.imgbtn:hover {
  background-color: #eee;
}
</style>
</head>
<body>
<?php if (count($tmp) == 0) { ?>
<div>Order not found. It may have been cancalled.</div>
<?php } else if ($tmp[0]->is_paid == 1) { ?>
<div>The order has been paid.</div>
<?php } else { ?>
  <?php if ($my_order != 0) { ?>
  <div id="dev_order" style="margin: 5px 0;">
    <script type="text/javascript">
      function update_height() {
        var ifm = document.getElementById("iframe_order");
        var dev = document.getElementById("dev_order");
        if (ifm && !window.opera){
          if (ifm.contentDocument && ifm.contentDocument.body){
            var body = ifm.contentDocument.body;
            var html = ifm.contentDocument.documentElement;
            ifm.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
            dev.style.height = Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight) + 'px';
          } else if (document.all('iframe_order') && iframe_order.document.body) {
            document.all('iframe_order').height = iframe_order.document.body.scrollHeight;
          }
        }
        parent.updateHeight_<?php echo $instance_id; ?>();
      }
    </script>
    <iframe id="iframe_order" onload="update_height()" width="100%" scrolling="no" frameborder="0"  marginwidth="0" marginheight="0" allowtransparency="true" src="../../../incls/php/ecomorder.print.php?id=<?php echo $my_order; ?>"></iframe>
  </div>
  
  <?php } else { ?>
  <table cellpadding=0 cellspacing=0>
    <tr>
      <td style="width: 730px;vertical-align: top;">
        <table style="width: 100%" cellpadding=0 cellspacing=0>
          <tr>
            <td>
              <img src="images/paynow.png"></img>
            </td>
            <td style="padding: 0 15px;">&nbsp;</td>
            <td style="border-left: 1px solid #ddd;">&nbsp;</td>
            <td style="padding: 0 15px;">&nbsp;</td>
            <td>
              <div style="margin: 0 auto;width: 100%;background: #ddd;border: 1px solid #aaa;padding: 10px 10px;">
                <div style="background: #eee;border: 1px solid #ccc;padding: 25px 40px;">
                  <div style="font-weight: bold;font-size: 18px;">Order No.</div>
                  <div style="font-weight: bold;color: #666;font-size: 18px;font-family: Lucida Sans Unicode;"><?php echo $tmp[0]->order_number; ?></div>
                  <div>&nbsp;</div>
                  <div style="font-weight: bold;font-size: 18px;">Total Price</div>
                  <div style="font-weight: bold;color: #ff8000;font-size: 18px;font-family: Lucida Sans Unicode;">$<?php echo $tmp[0]->total; ?></div>
                  <div>&nbsp;</div>
                  <div><a href="#" style="color: #000;" onclick="window.open('http://<?php echo URL . '?i=' . $page_id . '&script=index_paynow&pay=' . $order_id . '&my_order=' . $order_id; ?>');">Order Details</a></div>
                </div>
              </div>
            </td>
          </tr>
        </table>
        <div>&nbsp;</div>
        <div style="margin: 0 auto;text-align: left;width: 100%;background: #aaa;border: 1px solid #999;padding: 10px 10px;">
          <div style="background: #f8f8f8;border: 1px solid #666;padding: 25px 40px;">
            <table cellpadding=0 cellspacing=0>
              <tr>
                <td style="vertical-align: top;">
                  <div><b>YOU HAVE CHOSEN</b></div>
                  <div><?php echo $tmp[0]->payment_name; ?></div>
                </td>
                <td style="padding: 0 25px;">&nbsp;</td>
                <td style="border-left: 1px solid #666;">&nbsp;</td>
                <td style="padding: 0 25px;">&nbsp;</td>
                <td style="vertical-align: top;">
                  <div><?php echo $tmp[0]->_description; ?></div>
                  <?php if ($tmp[0]->payment_id == PAYPAL_ID) { ?>
                  <div>
                    <form id="paypal_form" method="post" action="<?php echo PAYPAL_URL; ?>" target="_parent">
                      <input type="hidden" name="cmd" value="_xclick"></input>
                      
                      <input type="hidden" name="business" value="<?php echo 'ns.gresource@gmail.com'; ?>"></input>
                      <input type="hidden" name="item_name" value="<?php echo $tmp[0]->order_number; ?>"></input> 
                      <input type="hidden" name="item_number" value=""></input>
                      <input type="hidden" name="amount" value="<?php echo $tmp[0]->total; ?>"></input>
                      <input type="hidden" name="currency_code" value="<?php echo 'NZD'; ?>"></input> 
                      
                      <input type="hidden" name="no_shipping" value="1"></input> 
                      <input type="hidden" name="no_note" value="1"></input> 
                      
                      <input type="hidden" name="charset" value="utf-8"></input> 
                      <input type="hidden" name="first_name" value="<?php echo $tmp[0]->first_name; ?>">
                      <input type="hidden" name="last_name" value="<?php echo $tmp[0]->last_name; ?>">
                      <input type="hidden" name="address1" value="<?php echo ($tmp[0]->address1); ?>">
                      <input type="hidden" name="address2" value="<?php echo ($tmp[0]->address2); ?>">
                      <input type="hidden" name="city" value="<?php echo $tmp[0]->city; ?>">
                      <input type="hidden" name="country" value="<?php echo $tmp[0]->country; ?>">
                      <input type="hidden" name="state" value="Not Applicable">
                      <input type="hidden" name="zip" value="<?php echo $tmp[0]->zip; ?>">
                      
                      <input type="hidden" name="night_phone_a" value="">
                      <input type="hidden" name="night_phone_b" value="<?php echo $tmp[0]->phone; ?>">
                      <input type="hidden" name="night_phone_c" value="">
                      
                      <input type="hidden" name="email" value="<?php echo $tmp[0]->email; ?>">
    
                      <input type="hidden" name="notify_url" value="<?php echo 'http://' . URL . 'incls/php/ipn.php'; ?>"></input>
                      <input type="hidden" name="return" value="http://<?php echo URL . '?i=' . ACCOUNT_ID . '&script=index_order&status=2'; ?>" /></input>
                      <input type="hidden" name="cancel_return" value="http://<?php echo URL . '?i=' . $page_id . '&script=index_paynow&pay=' . $order_id; ?>"></input>
    
                      <input type='submit' value="Pay it now" class="imgbtn"></input></div>
                    </form>
                  </div>
                  <?php } ?>
                </td>
              </tr>
            </table>
          </td>
          <td style="width: 230px;vertical-align: top;"><?php include dirname(__FILE__) . '/inc.right.php'?></td>
        </tr>
      </table>
    </div>
  </div>
  <?php } ?>
<?php } ?>
</body>
</html>